use "$out/HS0temp1_bnoSext_2022Sept.dta", clear

merge 1:1 anon t using "$in/admin3_alap.dta", keep(master match) nogen keepusing(vallazon1 ev w1 wh1 kor ferfi fogvisz1)

* construct EMP (employment) and NONEMP (non-employment) variables *
gen     EMP = 1 if (vallazon1!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (sick_leave_days<6 | sick_leave_days==.) & (fogvisz1==201)
	
codebook anon	
	
drop if HS==1

codebook anon

* flag one observation randomly within each anon *
	set seed 54321
	generate random = uniform()
	by anon (random), sort: gen flag = _n == 1
* gen m for the flagged time period, within each anon
gen m = 0 if flag==1

gen tm = t if flag==1
preserve
	keep if flag==1
	keep anon tm
	rename tm mintm
	sort anon
	tempfile tm
	save `tm'
restore
sort anon
merge m:1 anon using `tm'
drop if _m==2
drop _merge

replace m = t-mintm
drop mintm

gen korm = kor if flag==1
preserve
	keep if flag==1
	keep anon korm
	rename korm minkorm
	sort anon
	tempfile korm
	save `korm'
restore
sort anon
merge m:1 anon using `korm'
drop if _m==2
drop _merge

keep if minkorm>=20 & minkorm<=50
drop minkorm

codebook anon

drop if (m<-24 | m>24) & m!=.

* keep if the year corresponding to m=0 is at least 2009 *
gen yearm = ev if m==0
preserve
	keep if m==0
	keep anon yearm
	rename yearm mintyearm
	sort anon
	tempfile yearm
	save `yearm'
restore
sort anon
merge m:1 anon using `yearm'
drop if _m==2
drop _merge
drop if mintyearm<2009
drop yearm mintyearm

codebook anon

sort anon t
* get the first month of EMP after the shock
egen firstEMPtemp = min(m) if m>0 & EMP==1, by(anon)	
egen firstEMP = max(firstEMPtemp), by(anon)
drop firstEMPtemp

egen EMPm0 = mean(EMP) if m==0, by(anon) 
* keep all anons (and all their observations) for whom either Wml1==1 | Wm0==1
foreach var of varlist EMPm0 {
	egen max`var' = max(`var'), by(anon)
}

keep if (maxEMPm0 == 1)

gen ABS = 1 if firstEMP>=3 & firstEMP<=6
	gen ABSd=firstEMP if ABS==1
	
replace ABS = 0 if firstEMP==1 | firstEMP==2
	* for these, choose d randomly using those moths in which EMP=1*
	gen countEMP = m==0 & f3.EMP==1
	replace countEMP=. if m!=0
	assert countEMP==1 if ABS==1 & firstEMP==3 & m==0
	foreach X of numlist 4/6{
		replace countEMP=countEMP+1 if m==0 & f`X'.EMP==1
	}
	assert countEMP==1 if ABS==1 & firstEMP==6 & m==0
	set seed 12345
	drop random
	gen random = runiform(0,1) if m==0 & countEMP>0 & countEMP!=.
	replace random=int((random*(countEMP))+1)
	tab random countEMP
	gen ABSd_=.
	gen count=0
	foreach X of numlist 3/6{
		replace count=count+1 if ABS==0 & m==0 & f`X'.EMP==1 
		replace ABSd_=`X' if ABS==0 & m==0 & f`X'.EMP==1 & random==count
	}
	
	egen lastEMPtemp = max(m) if m>0 & EMP==1 & m<=6, by(anon)	
	egen lastEMP = max(lastEMPtemp), by(anon)
	drop lastEMPtemp	
	
	assert ABSd_==. if m==0 & ABS==0 & (firstEMP==1 | firstEMP==2) & lastEMP<=2
	
	egen ABSd_temp=max(ABSd_) if ABS==0, by(anon)
	assert ABSd_temp==ABSd_ if m==0 & ABS==0
	replace ABSd=ABSd_temp if ABS==0
	drop ABSd_ ABSd_temp
		
keep if ABS!=.

keep if m==0
keep anon t ev ABS ABSd

sum

save "$out/potential_controls_updated_tlm_bnoSext2_SL5_d3_6_in_EMP_2023Nov22.dta"
